package com.xjj.array;

public class E0066PlusOne {

    public int[] plusOne(int[] digits) {
        int offset = 1;
        for (int i = digits.length - 1; i >= 0; i--) {
            int sum = digits[i] + offset;
            digits[i] = sum % 10;
            offset = sum / 10;
        }
        if (offset != 0) {
            int[] result = new int[digits.length + 1];
            result[0] = offset;
            System.arraycopy(digits, 0, result, 1, digits.length);
            return result;
        } else {
            return digits;
        }
    }

}
